﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FBWMS.Properties;
using CrystalDecisions.CrystalReports.Engine;
using FBWMS.DataBase;
using System.Reflection;


namespace FBWMS
{
    public partial class F_LGELabelDataImport : Form
    {
        public F_LGELabelDataImport()
        {
            InitializeComponent();
        }

        #region "Variable"

        private DataTable dt_Total;
        private DataTable dt_Label;


        #endregion

        #region "Methods"

        private void P_Clear()
        {
            if (dt_Total != null)
            {
                dt_Total.Clear();
            }

            if (dt_Label != null)
            {
                dt_Label.Clear();
            }

            this.fpSpread1.ActiveSheet.RowCount = 0;

            this.txt_Input.Text = string.Empty;
            this.txt_Output.Text = string.Empty;
            this.txt_Error.Text = string.Empty;
            this.btn_Print.Enabled = false;
        }

        private void P_Input()
        {
            string str_Message = string.Empty;
            string str_Caption = G_CommonClassSystem.G_System_GetMessage(G_CommonDefine.G_Message_Information);
            try
            {
                string fileName = "";
                string str_Title = Resources.msg_PleaseChooseImportFile;
                openFileDialog1.Filter = "XLS files (*.XLS)|*.XLS";
                openFileDialog1.Title = str_Title;
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    fileName = openFileDialog1.FileName;
                }
                else
                {
                    MessageBox.Show(str_Title, str_Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                if (fileName.Substring(0, 2) != @"\\")
                {
                    MessageBox.Show("Please from server choose file.", str_Caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                int result = 0;
                string ErrMsg=string.Empty;
                int In_Cnt =0;
                int Out_Cnt = 0;
                int Err_Cnt = 0;


                result = G_CommonClassBussniss.G_F_LGELabelDataImport_DataImport(fileName, G_CommonDefine.G_PCNM,ref ErrMsg, ref In_Cnt, ref Out_Cnt,ref Err_Cnt);
    
               switch(result)
                {
                   case -1:
                        str_Message = Resources.msg_GroupIDExists;
                        MessageBox.Show(str_Message + ":" + ErrMsg, str_Caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        this.txt_Input.Text = "";
                        this.txt_Output.Text = "";
                        this.txt_Error.Text = "";
                        break;
                   case 0:

                        dt_Label = G_CommonClassBussniss.G_F_LGELabelDataEntry_GetData_Sub(G_CommonDefine.G_PCNM);

                        P_BindDataGrid(this.fpSpread1.ActiveSheet, dt_Label);
                        this.txt_Input.Text = In_Cnt.ToString();
                        this.txt_Output.Text = Out_Cnt.ToString();
                        this.txt_Error.Text = Err_Cnt.ToString();
                        str_Message = Resources.msg_DataImportSuccess;

                        MessageBox.Show(str_Message, str_Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);

                        this.btn_Print.Enabled = true;

                        break;
                   default:

                        str_Message = Resources.msg_DataImportFail;

                        MessageBox.Show(str_Message, str_Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.txt_Input.Text = "";
                        this.txt_Output.Text = "";
                        this.txt_Error.Text = "";
                        break;
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, str_Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void P_BindDataGrid(FarPoint.Win.Spread.SheetView sheet, DataTable dt)
        {

            if ((dt == null) || (dt.Rows.Count == 0))
            {
                sheet.RowCount = 0;
                return;
            }

            sheet.RowCount = dt.Rows.Count;


            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow row = dt.Rows[i];


                sheet.Cells[i, 0].Value = row["GP_NO"].ToString();
                sheet.Cells[i, 1].Value = row["LGEP_NO"].ToString();
                sheet.Cells[i, 2].Value = row["MakerP_NO"].ToString();
                sheet.Cells[i, 3].Value = row["Lot_NO"].ToString();
                sheet.Cells[i, 4].Value = row["SEQ_NO"].ToString();
                sheet.Cells[i, 5].Value = row["QTY"].ToString();
             
            }
        }

        #endregion

        #region "Events"

        private void F_LGELabelDataImport_Load(object sender, EventArgs e)
        {
            string str_Message = string.Empty;
            string str_Caption = G_CommonClassSystem.G_System_GetMessage(G_CommonDefine.G_Message_Error);
            try
            {
                dt_Total = new DataTable();
                dt_Label = new DataTable();

                G_CommonClassBussniss.G_F_LGELabelDataEntry_WorkerDataDelete(G_CommonDefine.G_PCNM);
                DataTable dt_Print = G_CommonClassBussniss.G_F_Nyk_Inspection_HK_GetPrinterData();
                G_CommonClassPrinter.G_LabelPrinter_ComboSetting(this.cmb_Printer, dt_Print);

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message,str_Caption,MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
        }

        private void btn_Clear_Click(object sender, EventArgs e)
        {
            P_Clear();
        }

        private void btn_Import_Click(object sender, EventArgs e)
        {
            string str_Caption = G_CommonClassSystem.G_System_GetMessage(G_CommonDefine.G_Message_Error);

            try
            {
                string str_Message = string.Empty;
                
                this.Cursor = Cursors.WaitCursor;
                P_Input();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, str_Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void btn_Print_Click(object sender, EventArgs e)
        {
            string str_Caption = G_CommonClassSystem.G_System_GetMessage(G_CommonDefine.G_Message_Error);

            try
            {
                string str_Message = string.Empty;

                this.Cursor = Cursors.WaitCursor;
                string str_GP_NO_temp = string.Empty;

                if (dt_Label == null || dt_Label.Rows.Count == 0)
                {
                    return;
                }
                ds_LGELabel ds_LGELabelData = G_CommonClassBussniss.G_PrintLGELabel(dt_Label);

                Assembly asm = Assembly.GetExecutingAssembly();
                Type t;
                Object obj;
                ReportDocument rpt;
                t = asm.GetType("FBWMS.Report." + "R_LGELabel");
                obj = t.InvokeMember(null, BindingFlags.CreateInstance, null, null, null);
                rpt = (ReportDocument)(obj);
                rpt.SetDataSource((DataSet)ds_LGELabelData);
                rpt.PrintOptions.PrinterName = this.cmb_Printer.Text.Trim();
                rpt.PrintToPrinter(1, true, 0, 0);

                str_Caption = G_CommonClassSystem.G_System_GetMessage(G_CommonDefine.G_Message_Information);
                MessageBox.Show(Resources.msg_PrintFinish, str_Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, str_Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        #endregion

    }
}
